package com.mnnu.controller;

import com.mnnu.anno.Log;
import com.mnnu.pojo.Emp;
import com.mnnu.pojo.PageBean;
import com.mnnu.pojo.Result;
import com.mnnu.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询{}页数据，每页数据{}条", page, pageSize);

        PageBean empList = empService.page(page, pageSize, name, gender, begin, end);

        return Result.success(empList);
    }

    /**
     * 删除员工数据
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids) {
        log.info("根据id{}删除员工", ids);

        empService.delete(ids);
        return Result.success();
    }

    /**
     * 添加员工
     * @param emp
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Emp emp) {
        log.info("添加员工emp:{}", emp);

        empService.add(emp);
        return Result.success();
    }

    /**
     * 跟据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询员工信息，id：{}", id);

        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    /**
     * 根据id修改员工信息
     * @param emp
     * @return
     */
    @Log
    @PutMapping
    public Result updateById(@RequestBody Emp emp) {
        log.info("修改id为{}的员工信息为：{}", emp.getId(), emp );

        empService.update(emp);
        return Result.success();
    }
}
